package se.webbzon.boltzmann.game.sequence.builder.dialogue;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import se.webbzon.boltzmann.game.object.dialogue.layout.DialogueLayout;
import se.webbzon.boltzmann.game.object.dialogue.layout.IntegerDialogueLayout;
import se.webbzon.boltzmann.game.sequence.builder.SequenceBuilder;
import se.webbzon.oschi01.friendly.Friendly;

public class IntegerDialogueRowDeflation extends AbstractDialogueRowDeflation {
	
	private String prefix, postfix;
	
	private String minValue, maxValue;
	
	/*============================================================
	Constructors
	============================================================*/
	
	public IntegerDialogueRowDeflation() {};
	
	public IntegerDialogueRowDeflation(IntegerDialogueRow row) {
		super(row);
		prefix = row.getPrefix();
		postfix = row.getPostfix();
		minValue = row.getMinValue();
		maxValue = row.getMaxValue();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public void inflate(ByteBuffer buff) {
		super.inflate(buff);
		
		// Read from buffer
		byte[][] datas = new byte[4][];
		for (int i = 0; i < datas.length; i++) {
			byte[] data = new byte[buff.getShort()];
			buff.get(data, 0, data.length);
			datas[i] = data;
		}
		
		// Decode strings
		try {
			minValue = new String(datas[0],"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			minValue = "";
		}
		
		try {
			maxValue = new String(datas[1],"UTF-8");
		} catch (UnsupportedEncodingException e) {
			maxValue = "";
		}
		
		try {
			prefix = new String(datas[2],"UTF-8");
		} catch (UnsupportedEncodingException e1) {
			prefix = "";
		}
		
		try {
			postfix = new String(datas[3],"UTF-8");
		} catch (UnsupportedEncodingException e) {
			postfix = "";
		}
	}

	@Override public ByteBuffer deflate(int allocate) {
		String[] strings = new String[] {minValue, maxValue, prefix, postfix};
		byte[][] datas = new byte[strings.length][];
		int size = 2 * datas.length;
		
		// Encode strings
		for (int i = 0; i < datas.length; i++) {
			try {
				datas[i] = strings[i].getBytes("UTF-8");
				size += datas[i].length;
			} catch (UnsupportedEncodingException e) {
				datas[i] = new byte[0];
			}
		}
		
		ByteBuffer buff = super.deflate(allocate + 8 + size);
		
		// Write to buffer
		for (int i = 0; i < datas.length; i++) {
			buff.putShort((short) datas[i].length);
			buff.put(datas[i]);
		}
		
		return buff;
	}
	
	/*============================================================
	Friendly Methods
	============================================================*/

	@Override @Friendly AbstractDialogueRow makeRow(SequenceBuilder builder,
													DialogueLayout layout) {
		return new IntegerDialogueRow(	builder,
										(IntegerDialogueLayout) layout,
										getBackground(),
										getText(),
										prefix,postfix,
										minValue,maxValue);
	}

}
